const url = 'https://testweapp.whwangdoudou.cn/classic_games/game.php';
const post = params => {
    return new Promise(function(resolve, reject) {
        wx.request({
            url: url,
            data: params,
            method: 'POST',
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                // success网络请求成功
                if (res.statusCode != 200) {
                    reject({
                        error: '服务器忙，请稍后重试',
                        code: 500
                    });
                    return;
                }
                resolve(res.data);
            },
            fail: function(res) {
                reject({
                    error: '网络错误',
                    code: 0
                });
            }
        })
    })
};

function ifwj(zk) {
    let p = {
        m: 'check_zhuanke',
        openid: zk
    }
    post(p).then(res => {
        if (res.allowplay == 1) {
            wx.setStorageSync('ifwj', res.allowplay);
            wx.navigateTo({
                url: '../palyGame/palyGame',
            });
        }
    })
};

function getCurrentPageUrlWithArgs() {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var url = currentPage.route //当前页面url
    var options = currentPage.options //如果要获取url中所带的参数可以查看options

    //拼接url的参数
    var urlWithArgs = url + '?'
    for (var key in options) {
        var value = options[key]
        urlWithArgs += key + '=' + value + '&'
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

    return urlWithArgs
}

module.exports = {
    post: post,
    ifwj: ifwj,
    getCurrentPageUrlWithArgs: getCurrentPageUrlWithArgs
}